#include "../Include/io_pub.h"
#include "../Include/common_pub.h"
#include "../globals.h"
#include <stdio.h>
#include <string.h>

int load_students_from_file() {
    FILE* file = fopen("student.txt", "r");
    if (!file) {
        log_message(LOG_WARNING, "学生数据文件不存在");
        return SUCCESS;
    }
    
    student_count = 0;
    char id[20], name[50];
    int course_count_val;
    float total_credits;
    
    while (fscanf(file, "%s %s %d %f", id, name, &course_count_val, &total_credits) == 4) {
        if (student_count < MAX_STUDENTS) {
            strcpy(students[student_count].id, id);
            strcpy(students[student_count].name, name);
            students[student_count].course_count = course_count_val;
            students[student_count].total_credits = total_credits;
            student_count++;
        }
    }
    
    fclose(file);
    log_message(LOG_INFO, "从文件加载 %d 条学生数据", student_count);
    return SUCCESS;
}

int save_students_to_file() {
    FILE* file = fopen("student.txt", "w");
    if (!file) {
        log_message(LOG_ERROR, "无法打开学生数据文件");
        return ERROR;
    }
    
    for (int i = 0; i < student_count; i++) {
        fprintf(file, "%s %s %d %.1f\n", 
                students[i].id, students[i].name, 
                students[i].course_count, students[i].total_credits);
    }
    
    fclose(file);
    log_message(LOG_INFO, "保存 %d 条学生数据", student_count);
    return SUCCESS;
}

int load_courses_from_file() {
    FILE* file = fopen("class.txt", "r");
    if (!file) {
        log_message(LOG_WARNING, "课程数据文件不存在");
        return SUCCESS;
    }
    
    course_count = 0;
    char id[20], name[50], type[30];
    int hours, max_students, selected_count;
    float credit;
    
    while (fscanf(file, "%s %s %s %d %f %d %d", 
                  id, name, type, &hours, &credit, &max_students, &selected_count) == 7) {
        if (course_count < MAX_COURSES_NUM) {
            strcpy(courses[course_count].id, id);
            strcpy(courses[course_count].name, name);
            strcpy(courses[course_count].type, type);
            courses[course_count].hours = hours;
            courses[course_count].credit = credit;
            courses[course_count].max_students = max_students;
            courses[course_count].selected_count = selected_count;
            course_count++;
        }
    }
    
    fclose(file);
    log_message(LOG_INFO, "从文件加载 %d 条课程数据", course_count);
    return SUCCESS;
}

int save_courses_to_file() {
    FILE* file = fopen("class.txt", "w");
    if (!file) {
        log_message(LOG_ERROR, "无法打开课程数据文件");
        return ERROR;
    }
    
    for (int i = 0; i < course_count; i++) {
        fprintf(file, "%s %s %s %d %.1f %d %d\n",
                courses[i].id, courses[i].name, courses[i].type,
                courses[i].hours, courses[i].credit,
                courses[i].max_students, courses[i].selected_count);
    }
    
    fclose(file);
    log_message(LOG_INFO, "保存 %d 条课程数据", course_count);
    return SUCCESS;
}

int initialize_data(InputMode mode) {
    if (mode == INPUT_FROM_FILE) {
        load_students_from_file();
        load_courses_from_file();
    }
    return SUCCESS;
}
